package com.uetoken.cn.utils;

import android.content.Context;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.uetoken.cn.R;

/**
 * @author 谢杨
 * @time 2018/8/2 10:39
 * @class 描述:倒计时工具类
 */
public class CountDownTimerUtils extends CountDownTimer{
    /**
     * 显示倒计时的textview
     */
    private TextView mTextView;
    private Context  mContext;

    /**
     *
     * @param textView
     * @param millisInFuture  从开始调用start()到倒计时完成（单位:毫秒）
     * @param countDownInterval 接收onTick(long)回调的间隔时间。（单位:毫秒）
     */
    public CountDownTimerUtils(Context context, TextView textView,long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
        this.mContext = context;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //设置不可点击
        mTextView.setClickable(false);
        //设置倒计时时间
        mTextView.setText(millisUntilFinished / 1000 + "s");
        mTextView.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.darker_gray));
        mTextView.setTextColor(ContextCompat.getColor(mContext,android.R.color.white));
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onFinish() {
        mTextView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.tv_bg_blue_border));
        mTextView.setTextColor(ContextCompat.getColor(mContext,R.color.color_app_theme));
        mTextView.setText(mContext.getString(R.string.tv_get_verification_code));
        //重新获得点击
        mTextView.setClickable(true);
    }
}
